<template>
  <div>
    <el-form ref="form" label-width="100px">
      <el-form-item label="SPU名称"> {{ spu.spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格（元）">
        <el-input placeholder="价格（元）" v-model="skuInfo.price"></el-input>
      </el-form-item>
      <el-form-item label="重量（千克）">
        <el-input
          placeholder="重量（千克）"
          v-model="skuInfo.weight"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="(attr, index) in attrInfoList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="(attrValue, index) in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item
            :label="SaleAttr.saleAttrName"
            v-for="(SaleAttr, index) in SpuSaleList"
            :key="SaleAttr.id"
          >
            <el-select placeholder="请选择" v-model="SaleAttr.attrIdAndValueId">
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${SaleAttr.id}:${saleAttrValue.id}`"
                v-for="(saleAttrValue, index) in SaleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表">
        <el-table
          style="width: 100%"
          border
          :data="SpuImagesList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="width"> </el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template v-slot="{ row, $index }">
              <img :src="row.imgUrl" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template v-slot="{ row, $index }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      SpuImagesList: [],
      SpuSaleList: [],
      attrInfoList: [],
      skuInfo: {
        category3Id: 0,
        tmId: 0,
        price: 0,
        spuId: 0,
        skuName: "",
        weight: "",
        skuAttrValueList: [
          //  {
          //    attrId: 0,
          //   valueId: 0,
          //  },
        ],
        skuDefaultImg: "",
        skuDesc: "",
        skuImageList: [
          // {
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   spuImgId: 0,
          // },
        ],
        skuSaleAttrValueList: [
          // {
          //
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          // },
        ],
      },
      spu: {},
      //选中图片的数据
      imageList: [],
    };
  },
  methods: {
    //发送请求，得到数据
    async getDate(Category1Id, Category2Id, spu) {
      //收集数据
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spu = spu;
      //获得图片的数据
      let result1 = await this.$API.spu.reqSpuImagesList(spu.id);
      // console.log(result1)
      if (result1.code == 200) {
        let list = result1.data;
        list.forEach((element) => {
          element.isDefault = 0;
        });
        this.SpuImagesList = list;
      }
      //获得销售属性
      let result2 = await this.$API.spu.reqSpuSaleList(spu.id);
      if (result2.code == 200) {
        this.SpuSaleList = result2.data;
      }

      // 获得平台属性的数据
      let result3 = await this.$API.spu.reqAttrInfoList(
        Category1Id,
        Category2Id,
        spu.category3Id
      );
      if (result3.code == 200) {
        this.attrInfoList = result3.data;
      }
    },
    //处理复选框选择的回调
    handleSelectionChange(selection) {
      // console.log(selection);
      this.imageList = selection;
    },
    //进行排他操作 设置默认图片
    changeDefault(row) {
      this.SpuImagesList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    //取消按钮的回调
    cancel() {
      this.$emit("changescenes", 0);
    },
    async save() {
      const { skuInfo, SpuSaleList, attrInfoList, SpuImagesList } = this;
      //整理平台属性
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);
      //整理销售属性
      skuInfo.skuSaleAttrValueList = SpuSaleList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] =
            item.attrIdAndValueId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      //整理图片属性
      skuInfo.skuImageList = SpuImagesList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
      let result = await this.$API.spu.reqAddSku(skuInfo);
      // console.log(result);
      if (result.code == 200) {
        this.$message({ type: "success", message: "保存成功" });
        Object.assign(this._data, this.$options.data());
           this.$emit("changescenes", 0);
      }
    },
  },
};
</script>

<style>
</style>